home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / windows4 / pcproj.zip / NETWORK.CLS < prev    next >
Text File  |  1988-11-30  |  5KB  |  186 lines

  1. /* The Network class is a formal class which defines basic
  2.    connection methods for nodes. The network maintains the
  3.    start and end nodes as well as two dictionaries of nodes.
  4.    NodeNames uses the name as key, display uses the relative
  5.    position as key for easy window "hit testing" etc. */!!
  6.  
  7. inherit(Object, #Network, #(name       /* network name */
  8. desc       /* longer description */
  9. start      /* starting node */
  10. end        /* ending node */
  11. nodeNames  /* dictionary by name */
  12. display    /* dictionary by posn */), 2, nil)!!
  13.  
  14. now(NetworkClass)!!
  15.  
  16. /* Create a new network. */
  17. Def  new(self)
  18.   ^init(new(self:Behavior));
  19. }  !!    
  20.  
  21. now(Network)!!
  22.  
  23. /* Return the node if the name exists in the
  24.    nodeNames dictionary, otherwise return false and
  25.    display an error message. */
  26. Def  checkNodeExists(self, nodeName | node)
  27. {
  28.   if not(node := nodeExists(self, nodeName))
  29.     beep();
  30.     errorBox(loadString(PW_NODEINVAL), 
  31.       nodeName + loadString(PW_NOTEXIST));
  32.   endif;
  33.   ^node;
  34. }!!
  35.  
  36. /* Adjust the display as necessary.
  37.    Called by setPosn, resetPosn in class Node. */
  38. Def  resetPosn(self, aNode, oldPos)
  39. {    
  40.   if (oldPos in display) = aNode
  41.     remove(display, oldPos);
  42.   endif;
  43.   
  44.   /* if there is a conflict adjust as necessary */
  45.   /* the resulting diagram may not be perfect */
  46.   
  47.   if pos(aNode) in display      /* conflict */
  48.     if aNode.y > 0              /* not top row */
  49.       aNode.y := aNode.y - 1;   /* go up */
  50.     endif;
  51.     loop 
  52.     while pos(aNode) in display
  53.       aNode.y := aNode.y + 1;   /* go down */
  54.     endLoop;
  55.   endif;
  56.    
  57.   add(display, pos(aNode), aNode);
  58. }!!
  59.  
  60. /* Return the number of nodes in the network. */
  61. Def  size(self)
  62. {
  63.   ^size(nodeNames);
  64. }!!
  65.  
  66. /* Given a point position, lookup the node. */
  67. Def  displayLookup(self, pos)
  68.   ^display[pos];
  69. }!!
  70.  
  71. /* Return a dictionary of nodes. */
  72. Def  nodes(self)
  73. {
  74.   ^nodeNames;
  75. }!!
  76.  
  77. /* Return the type of nodes that start and end should be.
  78.    Descendants will redefine this method. */
  79. Def  nodeClass(self)
  80. {
  81.   ^Node;
  82. }!!
  83.  
  84. /* Remove a node from the node table. */
  85. Def  removeNode(self, aNode)
  86.   remove(nodeNames, getName(aNode));
  87. }!!
  88.  
  89. /* Add a node to the node table.  Use it's name as the key. */
  90. Def  addNode(self, aNode)
  91.   add(nodeNames, getName(aNode), aNode);
  92. }!!
  93.  
  94. /* Check if the nodes name1 and name2 exist.  If so,
  95.    disconnect node1->node2.  */
  96. Def  disconnect(self, name1, name2 | node1, node2)
  97. {
  98.   node1 := checkNodeExists(self, name1);
  99.   node2 := checkNodeExists(self, name2);
  100.   if node1 cand node2
  101.     disconnect(node1, node2);
  102.   endif;
  103. }!!
  104.  
  105. /* Return the node if the name exists in the
  106.    nodeNames dictionary, otherwise return false.
  107.    If error checking is required, use checkNodeExists. */
  108. Def  nodeExists(self, nodeName | node)
  109. {
  110.   ^nodeName in nodeNames;
  111. }!!
  112.  
  113. /* Check if the nodes name1 and name2 exist.  If so,
  114.    connect node1->node2.  */
  115. Def  connect(self, name1, name2 | node1, node2)
  116. {
  117.   node1 := checkNodeExists(self, name1);
  118.   node2 := checkNodeExists(self, name2);
  119.   if node1 cand node2
  120.     connect(node1, node2);
  121.   endif;
  122. }!!
  123.  
  124. /* Get the description. */
  125. Def  getDesc(self)
  126. { ^desc;
  127. } !!
  128.  
  129. /* Show a diagram of the network.  Keep track
  130.    of visited nodes to avoid looping. 
  131.    Note: Only for use during development. */
  132. Def  show(self | visited)
  133.   printLine("");
  134.   printLine(self);            /* title */
  135.   CurPos:=0;                  /* global variable */
  136.   visited := new(Set,10);
  137.   add(visited,start); 
  138.   show(start, visited, 0);    /* begin recursing */
  139.   printLine("");
  140. }!!
  141.  
  142. /* Set the name. */
  143. Def  setName(self, aName)
  144. { name := aName;
  145. }  !! 
  146.  
  147. /* Get the name. */
  148. Def  getName(self)
  149. { ^name;
  150. } !!
  151.  
  152. /* Initialize a new Network. Create the start and end nodes
  153.    and add them to the dictionaries.  The dictionaries will
  154.    grow in size as necessary.  Uses the nodeClass method
  155.    to determine what type of nodes start and end should be.  */
  156. Def  init(self)
  157. { name := "";
  158.   desc := "";
  159.   start := new(nodeClass(self));
  160.   setName(start,"Start");
  161.   end := new(nodeClass(self));
  162.   setName(end,"End");
  163.   end.x := 0; end.y := 1;    /* default location */
  164.   start.network := end.network := self;
  165.   
  166.   nodeNames := new(Dictionary, 10);     /* by name */
  167.   nodeNames["Start"] := start;
  168.   nodeNames["End"] := end;
  169.   
  170.   display := new(Dictionary, 10);       /* by posn */
  171.   add(display, pos(start), start);
  172.   add(display, pos(end), end);
  173. } !! 
  174.  
  175. /* print the network name */
  176. Def  printOn(self, aStream)
  177. { printOn(asString(class(self)) + "(" + name + ")",
  178.   aStream);
  179. } !!
  180.  
  181.